﻿using System.Linq;
using System.Web.Mvc;
using TelerikConnect.Models;
using TelerikConnect.Web.DBConnect;
using TelerikConnect.Web.Edit;

namespace TelerikConnect.Web.Controllers
{
    [Authorize]
    public class SkillController : Controller
    {
        private readonly TelerikDb db = new TelerikDb();

        public ActionResult Add()
        {
            return PartialView("Add");
        }

        [HttpPost]
        public ActionResult Add(TextUpdateModel model)
        {
            string msg = "Sorry, there was a problem adding this skill. Please try again.";

            if (!db.Skills.Any(s => s.User.UserName == User.Identity.Name && s.Name == model.Content))
            {
                db.Skills.Add(new SkillModel()
                {
                    Name = model.Content,
                    User = db.UserProfiles.Where(u => u.UserName == User.Identity.Name).FirstOrDefault()
                }); // simplify later!
                db.SaveChanges();
                msg = string.Format("'{0}' was added to your skills!", model.Content);
            }

            ViewBag.Message = msg;

            return PartialView();
        }
    }
}